package com.cxhome.cxinfox.report;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.jfinal.plugin.activerecord.Record;
import com.traffic.managent.base.BaseService;

/**
 * 供应产品业务
 * 
 */
public class DayReportService extends BaseService {

	private final static Logger log = Logger.getLogger(DayReportService.class);
	public static final DayReportService service = new DayReportService();
	
	
	
	public List<Record> listReport(Map<String, Object> params) {
		params.put("startDate1", params.get("startDate"));
		params.put("endDate1", params.get("endDate"));
		params.put("customerId1", params.get("customerId"));
		params.put("departmentId1", params.get("departmentId"));
		List<Record> bills = DayReport.dao.listReport(params);
		if (bills != null && bills.size() > 0) {
			if ("adminReport".equals(params.get("action"))) {// 管理员登陆,加多"汇总"栏
				List<Record> bs = DayReport.dao.sumByDateAndOper(params);
				if (bs != null && !bs.isEmpty()) {
					for (Record record : bs) {
						BigDecimal dd = record.getBigDecimal("succeed").add(record.getBigDecimal("failed"));
						if (dd.doubleValue() > 0) {
							double d = record.getBigDecimal("succeed").divide(dd, 4, BigDecimal.ROUND_HALF_UP)
									.doubleValue();
							record.set("suRate", String.format("%.2f", d * 100));
						} else {
							record.set("suRate", "0");
						}
					}
				}
				bills.addAll(bs);
			}
			Record record = DayReport.dao.sumByDate(params);
			if (record != null) {
				BigDecimal dd = record.getBigDecimal("succeed").add(record.getBigDecimal("failed"));
				if (dd.doubleValue() > 0) {
					double d = record.getBigDecimal("succeed").divide(dd, 4, BigDecimal.ROUND_HALF_UP).doubleValue();
					record.set("suRate", String.format("%.2f", d * 100));
				} else {
					record.set("suRate", "0");
				}
				bills.add(record);
			}

			int i = 0;
			Record bill = bills.get(0);
			for (int j = 0; j < bills.size(); j++) {
				bill.set("reportDate", params.get("startDate") + "<br>~<br>" + params.get("endDate"));
				if (!bill.getStr("customerName").equals(bills.get(j).get("customerName"))) {
					if ("汇总".equals(bill.getStr("customerName"))) {
						bill.set("customerName",
								"<font style=\"color: green;\">" + bill.getStr("customerName") + "</font>");
						bill.set("reportDate", params.get("startDate") + "<br>~<br>" + params.get("endDate"));
					}
					bill.set("count", i);
					bill = bills.get(j);
					i = 0;
				}
				i++;
			}
			if (null == bill.get("count")) {
				bill.set("count", i);
			}
			bills.get(bills.size() - 1).set("customerName",
					"<font style=\"color: green;\">" + bill.getStr("customerName") + "</font>");
			bills.get(bills.size() - 1).set("reportDate",
					params.get("startDate") + "<br>~<br>" + params.get("endDate"));
		}
		return bills;
	}
}
